package com.meirimoney.app;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.meirimoney.sdk.models.Commodity;

/**
 * 话费充值
 * 
 * @author gewei
 * 
 */
public class PhoneRecharge extends BaseExchange
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.mrm_phone_recharge);
		this.setTitle();
		this.initializeUI(this.commodity);
	}

	/**
	 * 初始化当前界面的UI
	 */
	private void initializeUI(Commodity commodity)
	{
		if (null == this.hintTab)
		{
			this.hintTab = (TextView) this.findViewById(R.id.hintTab);
			String format = this.getString(R.string.phoneRechargeHintTab);
			this.hintTab.setText(String.format(format,
					String.valueOf(commodity.Price)));
		}
		if (null == this.mobileNumber)
		{
			this.mobileNumber = (EditText) this.findViewById(R.id.mobileNumber);
		}
		if (null == this.confirmMobileNumber)
		{
			this.confirmMobileNumber = (EditText) this
					.findViewById(R.id.confirmMobileNumber);
		}
		if (null == this.submitButton)
		{
			this.submitButton = (Button) this.findViewById(R.id.submit);
			this.submitButton.setOnClickListener(this);
		}
	}

	@Override
	public void onClick(View v)
	{
		if (this.checkBalance())
		{
			super.onClick(v);
			String number = this.mobileNumber.getText().toString();
			String confirmNumber = this.confirmMobileNumber.getText()
					.toString();
			if (this.checkForm(number, confirmNumber))
			{
				this.submit(1, number);
			}
		}
	}

	/**
	 * 检测当前表单输入的合法性
	 * 
	 * @return True：正确，False：不正确
	 */
	private boolean checkForm(String number, String confirmNumber)
	{
		if (number.equals(""))
		{
			this.showToastMessage("请输入手机号码");
		} else if (confirmNumber.equals(""))
		{
			this.showToastMessage("请再次输入手机号码");
		} else if (!number.equals(confirmNumber))
		{
			this.showToastMessage("两次手机号码输入不一致");
		} else
		{
			return true;
		}
		return false;
	}

	private EditText mobileNumber;

	private EditText confirmMobileNumber;
}
